package com.cbj.db_work.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cbj.db_work.entity.Player;
import com.cbj.db_work.entity.PlayerData;
import com.cbj.db_work.entity.vo.PlayDataVo;
import com.cbj.db_work.mapper.PlayerDataMapper;
import com.cbj.db_work.service.PlayerDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cbj.db_work.service.PlayerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cbj
 * @since 2021-07-01
 */
@Service
public class PlayerDataServiceImpl extends ServiceImpl<PlayerDataMapper, PlayerData> implements PlayerDataService {
    @Autowired
    private PlayerService playerService;
    @Override
    public boolean saveData(PlayDataVo playDataVo) {
        PlayerData playerData = new PlayerData();
        BeanUtils.copyProperties(playDataVo,playerData);
        int insert = baseMapper.insert(playerData);
        if(insert>0)
            return true;
        return false;
    }

    @Override
    public List<Integer> getData(String id) {
        QueryWrapper<PlayerData> wrapper = new QueryWrapper<>();
        wrapper.eq("player_id",id);
        PlayerData playerData = baseMapper.selectOne(wrapper);
        Integer assist = playerData.getAssist();
        Integer pass = playerData.getPass();
        Integer playingTime = playerData.getPlayingTime();
        Integer score = playerData.getScore();
        Integer shoot = playerData.getShoot();
        List<Integer> list = new ArrayList<>();
        list.add(score);
        list.add(pass);
        list.add(shoot);
        list.add(playingTime);
        list.add(assist);
        return list;
    }

    @Override
    public Map<String,List> rank(String factor) {
        QueryWrapper<PlayerData> wrapper = new QueryWrapper<>();
        if("".equals(factor)||factor==null){
            wrapper.orderByAsc("id");
        }
        wrapper.orderByAsc(factor);
        wrapper.select("player_id",factor);
        List<PlayerData> playerData = baseMapper.selectList(wrapper);
        List<Integer> data = new ArrayList<>();
        List<String> name = new ArrayList<>();
        for (PlayerData playerDatum : playerData) {
            String playerId = playerDatum.getPlayerId();
            Player byId = playerService.getById(playerId);
            String name1 = byId.getName();
            name.add(name1);
            if(factor.equals("assist")){
                Integer assist1 = playerDatum.getAssist();
                data.add(assist1);
            }else if(factor.equals("pass")){
                Integer pass1 = playerDatum.getPass();
                data.add(pass1);
            }else if(factor.equals("playing_time")){
                Integer playingTime1 = playerDatum.getPlayingTime();
                data.add(playingTime1);
            }else if(factor.equals("score")){
                Integer score1 = playerDatum.getScore();
                data.add(score1);
            }else if(factor.equals("shoot")){
                Integer shoot1 = playerDatum.getShoot();
                data.add(shoot1);
            }
        }
        HashMap<String, List> map = new HashMap();
        map.put("data",data);
        map.put("name",name);
        return map;
    }
}
